package com.mz.ozy.adapter;

import android.content.Context;
import android.media.Image;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.mz.ozy.R;
import com.mz.ozy.activity.home.ScheduledDeliveryActivity;
import com.mz.ozy.bean.SortBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/11/26.
 */

public class SortListAdapter extends RecyclerView.Adapter<SortListAdapter.MyViewHolder> {

    private Context mContext;
    private List<SortBean.ClassificationBean> mBeanList;
    private int selectPos;

    public SortListAdapter(Context context, List<SortBean.ClassificationBean> beanList) {
        mContext = context;
        this.mBeanList = beanList;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_sort_list, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
        SortBean.ClassificationBean classificationBean = mBeanList.get(position);
        holder.mTvItemName.setText(classificationBean.getName());
        if (position == 0) {
            holder.mIvRecommend.setVisibility(View.VISIBLE);
        } else {
            holder.mIvRecommend.setVisibility(View.GONE);
        }
        if (position == selectPos) {
            holder.mLlItemName.setBackgroundResource(R.drawable.bg_solid_73b21a_11dp);
            holder.mTvItemName.setTextColor(mContext.getResources().getColor(R.color.white));
        } else {
            holder.mLlItemName.setBackgroundResource(0);
            holder.mTvItemName.setTextColor(mContext.getResources().getColor(R.color.c_363636));

        }
        holder.mLlItemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectPos = position;
                notifyDataSetChanged();
                if (mContext instanceof ScheduledDeliveryActivity) {
                    ScheduledDeliveryActivity activity = (ScheduledDeliveryActivity) mContext;
                    activity.updateSortData(position, classificationBean);

                }
            }
        });
    }


    @Override
    public int getItemCount() {
        return mBeanList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_item_name)
        TextView mTvItemName;
        @BindView(R.id.iv_recommend)
        ImageView mIvRecommend;
        @BindView(R.id.ll_item_name)
        LinearLayout mLlItemName;
        @BindView(R.id.ll_itemView)
        LinearLayout mLlItemView;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

    }

}
